﻿using System;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class DefaultRowHeightRecord : WorksheetLevelRecord
    {
        private bool fUnsynced;
        private bool fDyZero;
        private bool fExAsc;
        private bool fExDsc;
        private ushort miyRwOrHidden = 300;

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            ushort u = context.ReadUInt16();
            fUnsynced = (u & 0x1) != 0;
            fDyZero = (u & 0x2) != 0;
            fExAsc = (u & 0x4) != 0;
            fExDsc = (u & 0x8) != 0;
                miyRwOrHidden = context.ReadUInt16();

        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.DefaultRowHeight; }
        }

        internal override void Apply(BiffReaderWorksheetContext context)
        {
            if (!fDyZero)
                context.DefaultRowHeight = ((double)miyRwOrHidden)/1440*96;
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            ushort u = 0;
            if (fUnsynced)
                u |= 0x1;
            if (fDyZero)
                u |= 0x2;
            if (fExAsc)
                u |= 0x4;
            if (fExDsc)
                u |= 0x8;
            context.Writer.Write(u);
            context.Writer.Write(miyRwOrHidden);
        }
    }
}